package org.antstudio.githook;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;


public class GitHooker {

	private ServerSocketChannel serverSocketChannel ;
	
	public static void main(String[] args) throws IOException {
		GitHooker gitHooker = new GitHooker();
		gitHooker.init(8989);
		gitHooker.receiveMessage();
	}
	
	
	public void init(int port) throws IOException{
		serverSocketChannel = ServerSocketChannel.open();
		serverSocketChannel.configureBlocking(false);
		serverSocketChannel.bind(new InetSocketAddress(8989));
	}
	
	public void receiveMessage() throws IOException{
		ByteBuffer buffer = ByteBuffer.allocate(1024);
		while(true){
			SocketChannel socketChannel = serverSocketChannel.accept();
			if(socketChannel != null){
				socketChannel.configureBlocking(false);
				while(socketChannel.read(buffer)>0){
					System.out.print(new String(buffer.array()));
					buffer.clear();
				}
				buffer.put("Content-type:text/html".getBytes());
				System.out.println(2);
				buffer.flip();
				socketChannel.write(buffer);
				socketChannel.close();
				buffer.clear();
			}
		}
	}
}
